﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TimKen.App;
using System.Data;
using TimKen.DataAccess;
using System.Data.SqlClient;

namespace TimKen.Web.Admin.Product
{
    public partial class Comment_Manage : AdminPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["ProductID"] != null && TypeParse.IsNumeric(Request.QueryString["ProductID"]))
                {
                    ProductID = Convert.ToInt32(Request.QueryString["ProductID"]);
                }
                BindData();
            }
        }
        public int ProductID
        {
            get
            {
                if (ViewState["ProductID"] != null && TypeParse.IsNumeric(ViewState["ProductID"]))
                    return Convert.ToInt32(ViewState["ProductID"]);
                else
                    return 0;
            }
            set
            {
                ViewState["ProductID"] = value;
            }
        }

        private void BindData()
        {
            string sql = "select  * from PComment WHERE  ProductID={0}";

            sql = string.Format(sql, ProductID);
            DataSet ds = DbHelperSQL.Query(sql);

            this.gvCommentList.DataSource = ds.Tables[0].DefaultView;
            this.gvCommentList.DataBind();
        }
        protected void btSave_Click(object sender, EventArgs e)
        {
            string content = this.txtContent.Text;
            string id = this.lablCommentID.Text;
            if (id != "")
            {
                string updatesql = "UPDATE  PComment  SET  [Content]='{0}' WHERE CommentID={1}";
                updatesql = string.Format(updatesql, content, id);
                DbHelperSQL.ExecuteSql(updatesql);
            }
            BindData();
        }

        protected void gvCommentList_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "del")
            {
                string sql = "delete from  PComment  WHERE CommentID={0}";
                sql = string.Format(sql, int.Parse(e.CommandArgument.ToString()));
                DataSet ds = DbHelperSQL.Query(sql);
                BindData();
                Response.Redirect(Request.RawUrl);
            }
            else if (e.CommandName == "mod")
            {
                string sql = "select *  FROM   PComment  WHERE CommentID={0}";
                sql = string.Format(sql, int.Parse(e.CommandArgument.ToString()));

                SqlDataReader reader = DbHelperSQL.ExecuteReader(sql);

                if (reader.Read())
                {
                    this.lablCommentID.Text = reader["CommentID"].ToString();
                    this.lblCreator.Text = reader["Creator"].ToString();
                    this.lblCreate.Text = reader["CreateTime"].ToString();
                    this.txtContent.Text = reader["Content"].ToString();
                }
                reader.Close();

            }
        }
    }
}
